﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BBMS.DLL.Forms.Base;
using BBMS.DLL.DataContract;
using BBMS.DLL.Base;
using ExcelLibrary.SpreadSheet;
namespace BBMS.DLL.Forms.Catalog.SupplierType
{
    public partial class frmSupplierTypeFilter : FrmFilterBase
    {
        public frmSupplierTypeFilter()
        {
            InitializeComponent();
        }
        public new static readonly string FunctionKey = "DMLoaiNCC";
        protected override string GetFunctionKey() { return "DMLoaiNCC"; }
        protected override ObjectBase GetObjectBase() { return new BBMS.DLL.DataContract.LoaiNhaCungCap(); }

        protected override void GenColHeaders()
        {
            lvList.Clear();
            lvList.Columns.Add("Mã", 80);
            lvList.Columns.Add("Tên loại NCC", 250);
            lvList.Columns.Add("Mức ưu tiên", 250);
            lvList.Columns.Add("Theo thời vụ", 250);
        }
        protected override ArrayList GenSampleData()
        {
            ArrayList ar = new ArrayList();
            ar.Add(new string[] {"Mã loại","Tên loại NCC", "Mức ưu tiên", "Theo thời vụ"});
            LoaiNhaCungCap loaiNhaCungCapList = new LoaiNhaCungCap();
            ArrayList arr = loaiNhaCungCapList.GetAllObject(fParams);
            if (arr != null)
            {
                for (int i = 0; i < arr.Count; i++)
                {
                    LoaiNhaCungCap lncc = (LoaiNhaCungCap)arr[i];
                    ar.Add(new string[] { lncc.Ma, lncc.Ten, lncc.MucUuTien.ToString(), lncc.TheoThoiVu.ToString() });
                }
            }
          //  ar.Add(new string[] { "Loại A", "Loại A", "1"});
            return ar;
        }

        protected override ArrayList GetImportedList(Worksheet sheet)
        {
            ArrayList arret = new ArrayList();
            for (int i = 1; i < sheet.Cells.Rows.Count; i++)
            {
                LoaiNhaCungCap lncc = new LoaiNhaCungCap();
                Cell cell = sheet.Cells[i, 0];
                lncc.Ma = cell.Value.ToString();
                cell = sheet.Cells[i, 1];
                lncc.Ten = cell.Value.ToString();
                cell = sheet.Cells[i, 2];
                if (cell.Value.ToString() != "") lncc.MucUuTien = Convert.ToInt32(cell.Value.ToString());
                cell = sheet.Cells[i, 3];
                if (cell.Value.ToString() != "") lncc.TheoThoiVu = Convert.ToBoolean(cell.Value.ToString());
                arret.Add(lncc);

            }
            return arret;
        }

        protected override void UpdateFilterParams()
        {
            if (txtMa.Text.Trim() != "")
            {
                fParams = new FilterParams();
                if (txtMa.Text.Trim() != "")
                {
                    FilterCondition fcMa = new FilterCondition("Ma", "Ma", "%" + txtMa.Text.Trim() + "%", SQLOperator.LIKE);
                    fParams.ArParams.Add(fcMa);
                }

                fParams.CurrentPage = 0;                
            }
            else
            {
                MessageBox.Show(null, "Bạn phải nhập nội dung tìm kiếm", "Thông báo");
            }
        }

        protected override FrmEditBase CreateEditForm(ObjectBase obj)
        {
            return new frmSupplierTypeUpdate(obj);
        }

        protected override void AddNewObjectToList(BBMS.DLL.Base.ObjectBase obj)
        {
            ListViewItem it = new ListViewItem(((LoaiNhaCungCap)obj).Ma);
            it.SubItems.Add(((LoaiNhaCungCap)obj).Ten);
            it.SubItems.Add(((LoaiNhaCungCap)obj).MucUuTien.ToString());
            if (((LoaiNhaCungCap)obj).TheoThoiVu) it.SubItems.Add("Có");
            else it.SubItems.Add("Không");
            it.Tag = obj;
            lvList.Items.Add(it);
        }

        protected override void UpdateObjectToList(BBMS.DLL.Base.ObjectBase obj)
        {
            foreach (ListViewItem it in lvList.Items)
            {
                if (((LoaiNhaCungCap)it.Tag).Ma == ((LoaiNhaCungCap)obj).Ma)
                {
                    it.SubItems[1].Text = ((LoaiNhaCungCap)obj).Ten;
                    it.SubItems[2].Text = ((LoaiNhaCungCap)obj).MucUuTien.ToString();
                    if (((LoaiNhaCungCap)obj).TheoThoiVu) it.SubItems[3].Text ="Có";
                    else it.SubItems[3].Text ="Không";
                    it.Tag = obj;
                    break;
                }
            }
        }

      
    }
}
